<?php
namespace Index\Controller;

class MemberController extends CommonController{

    protected $_needLogin = true;

    //我的书架
    public function myBookShelf(){
        $Shelf = new \Common\Model\BookShelfModel();

        $this->assign('shelfs',$Shelf->getShelfById($this->_id));
        $this->display();
    }

    //添加书到我的书架
    public function addBookToShelf(){
       $book_id = I('request.book_id');
       $article_id = I('request.article_id');
       if(IS_AJAX && $this->saveShelf($book_id,$article_id)){
          exit('success');
       }

    }

    private function saveShelf($book_id,$article_id){
      if($article_id > 0){
          $Article = new \Common\Model\BookArticleModel();
          $ainfo = $Article->getOneList($article_id,'book_id');
          if(empty($ainfo['book_id'])){
              return false;
          }
          $data = array(
               'book_id'=>$ainfo['book_id'],
               'member_id'=>  $this->_id,
               'article_id'=>$article_id,
               'time'=>  time(),
           );

      }elseif($book_id > 0){
          $data = array(
               'book_id'=>$book_id,
               'member_id'=>  $this->_id,
               'time'=>  time(),
           );

      }
      $Shelf = new \Common\Model\BookShelfModel();
      if(!$Shelf->checkReData($data)){
          exit('over');
      }

      $yi_nums = $Shelf->getMyshelfNums($this->_id);
      if($yi_nums >= $this->_Info['shelf_nums']){
          exit('overlv');
      }
      if($Shelf->saveData($data,array('member_id'=>$this->_id,'book_id'=>$data['book_id']))){
           return true;
       }
       return false;

    }

    //从我的书架中删除书
    public function delBookForShelf(){
       $shelf_id = I('post.shelf_id');
       if($shelf_id > 0){
          $Shelf = new \Common\Model\BookShelfModel();
          if($Shelf->where("`shelf_id`='{$shelf_id}' AND `member_id`='{$this->_id}'")->delete()){
              exit('success');
          }
       }
     }

    //个人资料信息
    public function myInfo(){
        //\Common\Library\COM\MemberDo::addMemberPoint($this->_id,100,'rrrrrr');exit;
        $Members = new \Common\Model\MembersModel();



        $this->assign('info',$Members->getDetailInfo($this->_id));
        $this->display();
    }

    public function editInfo(){
        $Members = new \Common\Model\MembersModel();

        $this->assign('info',$Members->getDetailInfo($this->_id));
        $this->display();
    }

    //完善个人资料
    public function updateMyInfo(){
        $Members = new \Common\Model\MembersModel();

        $this->display();
    }

    //我的信箱
    public function myMessages(){
        $Message = new \Common\Model\MessageModel();
        $this->assign('title','我的收件箱');

        $this->assign('messages',$Message->where("`disabled`='1' AND `re_member_id`='{$this->_id}'")->select());
        $this->display('Member/myMessages');
    }
    //我的发件箱
    public function sendMessage(){
        $Message = new \Common\Model\MessageModel();

        $this->assign('title','我的发件箱');
        $this->assign('action','sendMessage');
        $this->assign('messages',$Message->where("`disabled`='1' AND `member_id`='{$this->_id}' AND `reply_id` = '0'")->select());
        $this->display('Member/myMessages');
    }
    //新消息发送
    public function newMessage(){
        $message = I('post.content');
        $type = I('request.type');
        $code = I('post.checkcode');
        if(empty($message)){
          $this->display();exit;
        }
        if(mb_strlen($message) > 150){
            $this->layerIfreamRe('发送的信息过长，限150字内!','','error');
        }
        if(!$this->checkVerify($code)){
            $this->layerIfreamRe('验证码错误!','','error');
        }
        $Message = new \Common\Model\MessageModel();
        if($Message->autoCheckToken($_POST)){
            $data = array(
                  'member_id'=>$this->_id,
                  're_member_id'=>1,
                  'send_ip'=>get_client_ip(),
                  'send_time'=>time(),
                  'type'=>$type,
                  'message'=>$message
                );
              if($Message->add($data)){
                    $this->layerIfreamRe('发送成功!',C('SYS_SITE'));
              }

        }
        $this->layerIfreamRe('页面不存在!',C('SYS_SITE'),'error');
    }

    //会员删除信息
    public function delMessage(){
        $message_id = I('post.message_id');
        if(IS_AJAX && $message_id > 0){
            $Message = new \Common\Model\MessageModel();
            if($Message->updateField('disabled','2',$message_id)){
                exit('success');
            }
        }
    }

    //我的积分记录
    public function pointLog(){
        $Point = new \Common\Model\MemberPointModel();

        $this->assign('logs',$Point->where("`member_id`='{$this->_id}'")->order("`addtime` DESC")->select());
        $this->display();
    }

    //推荐投票
    public function bookTj(){
        $book_id = I('post.book_id');
        $article_id = I('post.article_id');
        if(IS_AJAX && $book_id > 0){
            $bookTj = new \Common\Model\BookTjModel();
            $data = array(
                  'book_id'=>$book_id,
                  'article_id'=>$article_id,
                  'member_id'=>$this->_id,
                  'ctime'=>time(),
                  'year'=>date('Y',time()),
                  'month'=>date('m',time()),
                  'day'=>date('d',time())
              );
            if(!$bookTj->checkOneTj($data)){
                exit('overBook');
            }
            $yi_nums = $bookTj->getTjNums($this->_id);
            if($yi_nums >= $this->_Info['tj_nums']){
                exit('overNums');
            }
            if($bookTj->saveTj($data)){
              //投票统计统计
              D('Common/Book')->where("`book_id`='{$book_id}'")->setInc('tj_num'); //推荐数加1
              exit('success');
            }
        }
    }


    //退出登录
    public function logout(){
        unset($_SESSION,$_POST,$_GET,$_COOKIE);
        session(null);
        session_destroy();
        $this->layerIfreamRe('退出成功',C('SYS_SITE'));
    }




}